new MusicTalk();
new MusicBrainz();
new Player();
    
Listen = {
	reload:false,
	timeout_id:null,
	timeout_var:null,
	mode_mini:false,
	getVideos: function(artist, title, hide) { 
      Tabs.get('videos',hide);
  
      if(document.getElementById('videos_c').innerHTML.length)
         return;
      
      videos = new YouTubeVids();
      videos.setTarget('videos_c');
      videos.search(artist,title);
    },
  getInfo: function() {
  	Tabs.get('track_talk');
  },
  play: function(track_pos){
  	$('mt_player').sendEvent('playitem',track_pos);
  },
  /*______________________DISPLAY PLAYER____________________________________*/
  displayPlayer: function(opt1){
  	var opt2 = {height:player.is_playlist?'140':'40',enablejs:true,callback:'javascript:Listen.track_cb'};
  	var opt = $merge(opt1,opt2); 
    player.configure(opt).write('player');
  },
  /*______________________RELOAD PLAYER____________________________________*/
  reloadPlayer: function(){
  	//player.track.current = null;
  	Listen.reload = true;
  	var opt = {src: player.getSrc()}
  	Listen.displayPlayer(opt);
  },
  /*______________________PLAYER CALLBACK____________________________________*/
  track_cb: function(track){
  	if(track.state == 'start') {
  		$('track_loader').style.display = 'none';
  		var options = {
  			target:null, 
      	onComplete: function(o, target) {
                    var r = o.responseXML.getElementsByTagName('result')[0]; 
                    if(!r || r.firstChild.nodeValue != 'OK') {
                        throw 'ERROR: '+r;
                    }
                  },
       	data:'op=stats&track_id='+track.id                            
      };
     // we wait 20 sec and increse the no of times the song was listened
     Listen.timeout_var = options;
     Listen.timeout_id = setTimeout("new Ajax().makeRequest('proxy.php', Listen.timeout_var)",20000);
  	}
  	else{
  		if(player.track_to_remove)
  			player.removeItem();
  	}
  },
  /*______________________UPDATE VIEW____________________________________*/
  updateView: function(){
  	if(player.track.current == player.track.info.id || !player.is_playlist || Listen.mini_mode)
	  	return;
			
		var options = {'target':null, 
                  onLoading: function(o, target){ 
                  	    $('track_loader').style.display = 'block'; 
                  	    mt.onLoading($('track_loader_c'),'Loading next song...');
                  	},
                  onComplete: function(o, target)
                    {
                      try{
                        var start = o.responseText.indexOf('<!--START-->');
                        var sep = o.responseText.indexOf('<!--SEP-->');
                        if(start > 0){
                          eval('var new_track_info = '+o.responseText.substring(0,start));
                          player.track.info = $merge(player.track.info,new_track_info);
                        }
                        $('track_info').innerHTML = o.responseText.substring(start+12,sep);
                        $('track_view').innerHTML = o.responseText.substring(sep+10);

                        if(player.track_current == null)
                          $('track_loader').style.display = 'none';

                        player.track.current = new_track_info.id;
                        var rem = 'rem_'+new_track_info.id;
                        var add = 'add_'+new_track_info.id;
                        var icon_target = 'favicon_'+((player.track.info.fav_user)?rem:add);
                        var to_hide = 'favicon_'+((player.track.info.fav_user)?add:rem);
                        var html = '<a';
                        if(player.track.info.fav_user != null) html += ' style="display:none;"';
    										html += ' id="favicon_add_'+new_track_info.id+'" href="javascript:mt.track.fav(0,'+new_track_info.id+')"';
    										html += ' title="Add song to your playlist"><img src="img/icons/fave.gif" alt="Add" /></a>';
    										html += '<a'; 
    										if(player.track.info.fav_user == null) html += ' style="display:none;"';
   											html += ' id="favicon_rem_'+new_track_info.id+'" href="javascript:mt.track.fav(1,'+new_track_info.id+')"'+
         												' title="Remove song from your playlist"><img src="img/icons/unfav.png" alt="Remove" /></a>';
         								
         								$('favicon').innerHTML = html;
         								
                        $('track_menu').setStyle('visibility','visible');

                       }
                       catch(e){alert(e)}
                    },
                   data:'update_track=1'
                            
          };
    $('track_menu').setStyle('visibility','hidden');            
    new Ajax().makeRequest('/index.php?view=track&action=listen&tid='+player.track.info.id, options);
 },
 /*______________________TOGGLE BETWEEN MINI/FULL MODE_________________________________*/
 toggleFullMode: function(){
 	 if(!Listen.mini_mode){
 	    $('track_info').effect('opacity',{duration:300,
        onComplete: function(){
          	$('track_info').setStyle('height','');
          	$('track_info').effect('height',{duration:600}).start(10);
          	Listen.mini_mode = true;
          	}	}).start(0);

     $('track_view').effect('opacity',{duration:300,
          onComplete: function(){
          	$('track_view').setStyle('display','none');
          	}	}).start(0);
    
    $('toggle_full_mode').setAttribute('title','Switch to full mode');
    $('toggle_full_mode').firstChild.setAttribute('src','/img/icons/full_mode.png');
 	 }
 	 else{
 	 	 	Listen.mini_mode = false;
      /*  
      * update the view
 	 		* in case the user toggles after she has
 	 		* listened to one or more track in mini_mode
 	 		*/
 	 		Listen.updateView();	
 	  	
 	  	$('track_info').setStyle('height','');
      $('track_info').setStyle('display','block');
      $('track_info').effect('opacity',{duration:300}).start(1);
    
     	$('services').setStyle('height','auto');
    	$('track_view').setStyle('display','block');
     	$('track_view').effect('opacity',{duration:300}).start(1);
    
      $('toggle_full_mode').setAttribute('title','Switch to mini mode');
      $('toggle_full_mode').firstChild.setAttribute('src','/img/icons/mini_mode.png');
 	 }
 }
}

//function getUpdate(){}
function getUpdate(typ,position,id,info){
	if(typ == 'item'){
		
		try{clearTimeout(Listen.timeout_id);}
		catch(e){ alert("Timeout errror, please ignore\n\n".e) }
		
		if(info && player.is_playlist){
			/* these are the track info extracted from the
			 * playlist. This step will be performed only in
			 * PLAYLIST mode.
			 */
			player.track.to_remove = false;
			var track_info = info.split(' - ');
			var title = track_info[0];
			var artist = track_info[1];
			var release = track_info[2];
			player.setTrackInfo({ 'id':id, 'title':title, 'artist':artist,'release':release,'position':position });
		}
		
		if(!player.is_playlist && !info){
			return player.loadFile(player.track.info);
		}
		else if(!Listen.reload || !position){
		  setTimeout("Listen.updateView()",(position?0:500));
		  Listen.reload = false;
		}
	}
}
